package org.hnxxxy.ebms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.mapper.RecruitStuMapper;
import org.hnxxxy.ebms.mapper.RoleMapper;
import org.hnxxxy.ebms.mapper.TeacherAdmissMapper;
import org.hnxxxy.ebms.pojo.Plan;
import org.hnxxxy.ebms.mapper.PlanMapper;
import org.hnxxxy.ebms.pojo.Role;
import org.hnxxxy.ebms.service.PlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hnxxxy.ebms.util.CommonUntil;
import org.hnxxxy.ebms.vo.PlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-10
 */
@Service
public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements PlanService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private TeacherAdmissMapper teacherAdmissMapper;

    @Autowired
    private RecruitStuMapper recruitStuMapper;

    @Autowired
    private PlanMapper planMapper;

    @Override
    public Result<String> add(Plan plan) {
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        plan.setRecruitStuId(role.getUserId());
        this.save(plan);
        return Result.success(200,"添加成功");
    }

    @Override
    public Result<Plan> getPlans() {
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recruit_stu_id",role.getUserId());
        Plan plan = this.getOne(queryWrapper);
        return Result.success(plan);
    }

    @Override
    public Result<IPage<PlanVo>> getTeaPlans(int page) {
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        IPage<PlanVo> planIPage = null;
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        // 通过老师id查询到地区id，
        List<Integer> admissId = teacherAdmissMapper.selectByTeaId(role.getUserId());
        // 通过地区id查询到学生id，
        List<Integer> recId = recruitStuMapper.selectByAdmissId(admissId);
        if (recId == null || recId.size() == 0){
            return Result.success(planIPage);
        }
        IPage<PlanVo> iPage = new Page<>(page,10);
        planIPage = planMapper.selectPlanPage(iPage, recId);
        return Result.success(planIPage);
    }

    @Override
    public Result<IPage<PlanVo>> getTeaPlanByName(String name,int page) {
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        // 通过老师id查询到地区id，
        List<Integer> admissId = teacherAdmissMapper.selectByTeaId(role.getUserId());
        // 通过地区id查询到学生id，
        List<Integer> recId = recruitStuMapper.selectByAdmissId(admissId);
        IPage<PlanVo> iPage = new Page<>(page,10);
        IPage<PlanVo> planIPage = planMapper.selectPlanPageByName(iPage, name,recId);
        return Result.success(planIPage);
    }


}
